package com.huiquan.management.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.management.constant.GradeContants;
import com.huiquan.management.dao.GradeDao;
import com.huiquan.management.dao.GradeParamDao;
import com.huiquan.management.domain.Grade;
import com.huiquan.management.domain.GradeParam;

@Service
public class GradeService extends BaseService {

	@Autowired
	private GradeDao gradeDao;
	@Autowired
	private GradeParamDao gradeParamDao;

	public ModelAndView sentenceDisplay() {

		Map<String, Object> param = new HashMap<>();
		param.put("type", GradeContants.TYPE_ANALYSIS_XBS_IMAGEOLOGY);

		Grade grade = gradeDao.retrieveObjectByMap(param);
		if (grade == null) {
			// 如果第一次访问还没有数据，默认添加一条
			grade = initGrade(GradeContants.TYPE_ANALYSIS_XBS_IMAGEOLOGY);
		}
		LOGGER.info("获取句子的等级配置，id=" + grade.getId());

		param.clear();
		param.put("gradeId", grade.getId());
		List<GradeParam> gradeParams = gradeParamDao.retrieveList(param);

		// 将参数根据类别分类
		TreeMap<Integer, TreeMap<Integer, String>> classifyParams = new TreeMap<>();
		List<Integer> paramTypeList = new ArrayList<>();

		setClassifyParams(grade, classifyParams, gradeParams, paramTypeList);
		// 如果有等级参数不存在则初始化
		if (!paramTypeList.isEmpty()) {
			for (Integer paramType : paramTypeList) {
				initSentenceGradeParams(grade, paramType);
			}
		}
		gradeParams = gradeParamDao.retrieveList(param);
		setClassifyParams(grade, classifyParams, gradeParams, paramTypeList);

		// 配置外包系统参数
		Map<String, Object> map = new HashMap<>();
		map.put("grade", grade);
		map.put("classifyParams", classifyParams);
		map.put("paramNameMap", GradeContants.PARAM_NAME_MAP);
		// TODO 增加对参数的校验信息

		return new ModelAndView("management/index", map);
	}

	private void setClassifyParams(Grade grade, TreeMap<Integer, TreeMap<Integer, String>> classifyParams,
			List<GradeParam> gradeParams, List<Integer> paramTypeList) {
		classifyParams.clear();
		paramTypeList.clear();
		paramTypeList.addAll(GradeContants.PARAM_NAME_MAP.keySet());

		for (GradeParam gradeParam : gradeParams) {
			TreeMap<Integer, String> subParams = classifyParams.get(gradeParam.getType());

			if (subParams == null) {
				if (!paramTypeList.contains(gradeParam.getType())) {
					LOGGER.info("等级参数配置有误，type=" + gradeParam.getType());
					continue;
				} else {
					subParams = new TreeMap<>();
					classifyParams.put(gradeParam.getType(), subParams);
					paramTypeList.remove(gradeParam.getType());
				}
			}

			subParams.put(gradeParam.getGrade(), gradeParam.getParam());
		}
	}

	private List<GradeParam> initSentenceGradeParams(Grade grade, int type) {
		Long gradeId = grade.getId();

		List<GradeParam> list = new ArrayList<>();
		for (int i = 1; i <= grade.getNum(); i++) {
			list.add(newGradeParam(gradeId, i, type, "", 1L));
		}

		gradeParamDao.batchInsert(list);

		return list;
	}

	private GradeParam newGradeParam(Long gradeId, int grade, int type, String param, Long modifierId) {

		GradeParam gradeParam = new GradeParam();
		gradeParam.setGradeId(gradeId);
		gradeParam.setGrade(grade);
		gradeParam.setType(type);
		gradeParam.setParam(param);
		gradeParam.setModifierId(modifierId);

		return gradeParam;
	}

	private Grade initGrade(int type) {
		Grade grade = new Grade();
		grade.setType(type);
		grade.setNum(1);
		grade.setModifierId(1L); // 默认系统管理员

		gradeDao.insert(grade);

		// 初始化具体参数
		for (int paramType : GradeContants.PARAM_NAME_MAP.keySet()) {
			initSentenceGradeParams(grade, paramType);
		}

		return grade;
	}

	public ReturnData updateParam(long gradeId, int paramType, int grade, String param, User user) {

		GradeParam gradeParam = new GradeParam();
		gradeParam.setGradeId(gradeId);
		gradeParam.setGrade(grade);
		gradeParam.setType(paramType);
		gradeParam.setParam(param);
		gradeParam.setModifierId(user.getUserId());

		gradeParamDao.update(gradeParam);

		return ReturnUtil.success();
	}

}
